import sys

from System import *
from System.IO import *

from GtdProperties import *
from ActiveXObject import *

def ReportException():
    try:
        import traceback
        print "Unexpected error: "
        print '-' * 60
        traceback.print_exc(file=sys.stdout)
        print '-' * 60
    except:
        print "Unknown error"

def FindFolderByFullPath(rootFolders, fullPath):
    fullPath = Path.GetFullPath(fullPath)
    parts = fullPath.Substring(2).Split('\\')
    currentFolders = rootFolders
    folder = None

    for dirName in parts:
        try:
            folder = currentFolders[dirName]
            currentFolders = folder.Folders
        except:
            folder = None
            break

    return folder

def NewId():
    return "%s-%s" % (Guid.NewGuid().ToString(), Guid.NewGuid().ToString())

def GetComObjectTypeName(object):
    from Microsoft.VisualBasic import Information
    return Information.TypeName(object)

def GetLinkItem(linkFolder, linkType, id):
    items = linkFolder.Items
    linkItem = None
    for i in items:
        if GetComObjectTypeName(i) == linkType:
            item = ActiveXObject(i)
            gtdProperties = GtdProperties(ActiveXObject(item.getUserProperties()))
            if gtdProperties.getGtd() and gtdProperties.getGtdId() == id:
                linkItem = item
                break

    return linkItem
